
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.adapter.GiftBagCollectAdapter;
import com.kugou.game.sdk.adapter.GiftBagCollectAdapter.GiftBtnClickListener;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.base.BaseListAdapter;
import com.kugou.game.sdk.base.BaseListFragment;
import com.kugou.game.sdk.core.GameManager;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.GiftBag;
import com.kugou.game.sdk.entity.GiftBagTaoHaoResponse;
import com.kugou.game.sdk.entity.GiftbagListResponse;
import com.kugou.game.sdk.entity.GiftbagReceiveResponse;
import com.kugou.game.sdk.statistics.StatCmd;
import com.kugou.game.sdk.statistics.StaticsManager;
import com.kugou.game.sdk.ui.activity.RegisterByPhoneActivity;
import com.kugou.game.sdk.ui.dialog.GiftMessageDialog;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;
import com.kugou.game.sdk.utils.ToastUtil;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

import java.util.ArrayList;

/**
 * 描述:全部礼包页面
 * 
 * @author liux
 * @since 2014-8-16 下午03:04:03
 */
@SuppressLint("NewApi")
public class TotalGiftBagFragment extends BaseListFragment<GiftBag> implements OnScrollListener,
        OnItemClickListener, GiftBtnClickListener {
    private static final int MSG_UI_GET_GIFT_BAG = 0;

    private static final int MSG_UI_GET_GIFT_BAG_SUCESS = 1;

    private static final int MSG_UI_GET_GIFT_BAG_FAILD = 2;

    private static final int MSG_BACK_TAO_HAO_BAG = 3;

    private static final int MSG_UI_TAO_HAO_START = 4;

    private static final int MSG_UI_TAO_HAO_SUCESS = 5;

    private static final int MSG_UI_TAO_HAO_FAILD = 6;

    private static final int MSG_BACK_GET_GIFT_BAG = 7;

    private ProgressDialog mDialog2;

    private ProgressDialog mDialog;

    private GiftBagCollectAdapter mAdapter;

    public static TotalGiftBagFragment newInstance() {
        TotalGiftBagFragment fragment = new TotalGiftBagFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    View emptyView;

    /**
     * 初始化界面信息
     */
    private void initView(View view) {
        mAdapter.setGiftBtnClickListener(this);
        emptyView = LayoutInflater.from(getActivity()).inflate(
                Rx.layout.kg_layout_load_empty_content, null);
    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);
        if (getActivity() == null) {
            return;
        }
        switch (msg.what) {
            case MSG_UI_NO_DATA:
                mTipsLayout.setCustomView(emptyView);
                mTipsLayout.show(TipsLayout.TYPE_CUSTOM_VIEW);
                break;
            case MSG_UI_TAO_HAO_START:
                mDialog2 = new ProgressDialog(getActivity());
                mDialog2.setMessage("数据加载中，请稍候...");
                mDialog2.show();
                break;
            case MSG_UI_GET_GIFT_BAG:
                mDialog = new ProgressDialog(getActivity());
                mDialog.setMessage("数据加载中，请稍候...");
                mDialog.show();
                break;
            case MSG_UI_GET_GIFT_BAG_FAILD:
                // 领取礼包失败
                mDialog.dismiss();
                ToastUtil.showMsg("礼包领取不成功，请重试");
                break;
            case MSG_UI_GET_GIFT_BAG_SUCESS:
                // 领取礼包成功
                mDialog.dismiss();
                mAdapter.notifyDataSetChanged();
                ToastUtil.showMsg("礼包领取成功！");
                getActivity().sendBroadcast(new Intent(SDKActions.ACTION_GET_NEW_GIFT_BAG));
                break;
            case MSG_UI_TAO_HAO_SUCESS:
                mDialog2.dismiss();
                mAdapter.notifyDataSetChanged();
                new GiftMessageDialog(getActivity(), msg.obj.toString()).show();
                break;
            case MSG_UI_TAO_HAO_FAILD:
                // 领取礼包失败
                mDialog2.dismiss();
                ToastUtil.showMsg("淘号失败,请重试");
                break;
            default:
                break;
        }
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        super.handleBackgroundMessage(msg);
        if (getActivity() == null) {
            return;
        }
        String username = "";
        try {
            username = UserManager.getInstance().getCurrentUserInternal().getAccountName();
        } catch (Exception e) {
            e.printStackTrace();
        }

        GiftBag giftbag;
        Message message;
        switch (msg.what) {
            case MSG_BACK_GET_GIFT_BAG:
                sendEmptyUiMessage(MSG_UI_GET_GIFT_BAG);
                message = new Message();
                giftbag = (GiftBag) msg.obj;

                GiftbagReceiveResponse response2 = GameManager.requestGiftBag(giftbag.getId(),
                        username);
                if (response2 != null && response2.isOk()) {
                    giftbag.setExistscardnumber(response2.getGift().getExistscardnumber());
                    giftbag.setExtstatus(response2.getGift().getExtstatus());
                    message.what = MSG_UI_GET_GIFT_BAG_SUCESS;
                    message.obj = giftbag.getId();
                } else {
                    message.what = MSG_UI_GET_GIFT_BAG_FAILD;
                }
                sendUiMessage(message);
                break;
            case MSG_BACK_TAO_HAO_BAG:
                sendEmptyUiMessage(MSG_UI_TAO_HAO_START);
                message = new Message();
                giftbag = (GiftBag) msg.obj;

                GiftBagTaoHaoResponse response = GameManager.taoGiftBag(giftbag.getId(), username);
                if (response != null && response.isOk()) {
                    giftbag.setExtstatus(response.getGift().getExtstatus());
                    message.what = MSG_UI_TAO_HAO_SUCESS;
                    message.obj = response.getGift().getExistscardnumber();
                } else {
                    message.what = MSG_UI_TAO_HAO_FAILD;
                }
                sendUiMessage(message);
                break;
        }

    }

    /**
     * 礼包列表item领取按钮点击回调
     */
    @Override
    public void onGiftBtnClickListener(View v, GiftBag info) {
        User user = UserManager.getInstance().getCurrentUserInternal();
        if (user == null) {
            return;
        }
        if (info.getExtstatus() == 2) {
            // 发送统计-淘号
            StaticsManager.sendCommonStatis(StatCmd.CMD_BI_TAO_GIFTBAG);

            // 正式账号才能进行淘号
            if (!user.isRegistered()) {
                Intent intent = new Intent(getActivity(), RegisterByPhoneActivity.class);
                intent.putExtra(BaseCommonTitleFragmentActivity.FROM_ACTION_KEY,
                        BaseCommonTitleFragmentActivity.FROM_GIFTBAG_REGISTER);
                startActivity(intent);
                ToastUtil.showMsg("注册用户才能进行淘号哦，赶紧注册吧~");
            } else {
                Message msg = new Message();
                msg.obj = info;
                msg.what = MSG_BACK_TAO_HAO_BAG;
                sendBackgroundMessage(msg);
            }
        } else if (!TextUtils.isEmpty(info.getExistscardnumber())) {
            ClipboardManager cManager = (ClipboardManager) getActivity().getSystemService(
                    Context.CLIPBOARD_SERVICE);
            cManager.setText(info.getExistscardnumber());
            ToastUtil.showMsg("兑换码复制成功");
        } else if (info.getExtstatus() == 0) {
            // 发送统计-领取礼包
            StaticsManager.sendCommonStatis(StatCmd.CMD_BI_GET_GIFTBAG);

            // 正式账号才能领取礼包
            if (!user.isRegistered()) {
                Intent intent = new Intent(getActivity(), RegisterByPhoneActivity.class);
                intent.putExtra(BaseCommonTitleFragmentActivity.FROM_ACTION_KEY,
                        BaseCommonTitleFragmentActivity.FROM_GIFTBAG_REGISTER);
                startActivity(intent);
                ToastUtil.showMsg("注册用户才能领取礼包哦，赶紧注册吧~");
            } else {
                Message msg = new Message();
                msg.obj = info;
                msg.what = MSG_BACK_GET_GIFT_BAG;
                sendBackgroundMessage(msg);
            }
        }
    }

    @Override
    protected int findLayoutId() {
        return Rx.layout.kg_fragment_giftbag_listview;
    }

    @Override
    protected BaseListAdapter<GiftBag> createAdapter() {
        mAdapter = new GiftBagCollectAdapter(getActivity(), mListView);
        return mAdapter;

    }

    @Override
    protected ArrayList<GiftBag> loadDatas() {
        String username = "";
        try {
            username = UserManager.getInstance().getCurrentUserInternal().getAccountName();
        } catch (Exception e) {
            e.printStackTrace();
        }

        GiftbagListResponse response = GameManager.requestTotalGiftBagList(getPageIndex(),
                PAGE_SIZE, username);
        if (response != null && response.isOk()) {
            ArrayList<GiftBag> datas = response.getGiftbagList();
            return datas;
        } else {
            return null;
        }
    }
}
